package com.pfzy.ui.activity.voice;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.pfzy.ui.R;
import com.pfzy.ui.log.MyLog;
import com.pfzy.ui.utils.CommonUtil;

public class ImRecordDialogManager {
    public static final int STATUS_NOMAL = 1;
    public static final int STATUS_RECORD_ERROR = 4;
    public static final int STATUS_UP_CANCEL = 2;
    public static final int STATUS_UP_SHORT = 3;
    private static final String TAG = "ImRecordDialogManager";
    @SuppressLint({"StaticFieldLeak"})
    public static Context context;
    private static AlertDialog dialog;
    @SuppressLint({"StaticFieldLeak"})
    private static View view;
    private static WXVoiceButton wxVoiceButton;

    public static synchronized void init(Context c2) {
        synchronized (ImRecordDialogManager.class) {
            if (dialog == null) {
                context = c2;
                dialog = initAlertDialog();
            }
        }
    }

    private static AlertDialog initAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.im_voice_record_dialog_default, null);
        builder.setView(view);
        dialog = builder.create();
        if (dialog.getWindow() != null) {
            dialog.getWindow().setDimAmount(0.0f);
        }
        ((TextView) view.findViewById(R.id.text_cancel)).setText(CommonUtil.getContext().getString(R.string.status_nomal));
        ((TextView) view.findViewById(R.id.im_voice_tv_short)).setText(CommonUtil.getContext().getString(R.string.im_voice_time_short));
        ((TextView) view.findViewById(R.id.im_voice_tv_release)).setText(CommonUtil.getContext().getString(R.string.release_hand_send));
        ((TextView) view.findViewById(R.id.text_cancel)).setText(CommonUtil.getContext().getString(R.string.status_nomal));
        ((TextView) view.findViewById(R.id.text_cancel)).setText(CommonUtil.getContext().getString(R.string.status_nomal));

        wxVoiceButton = view.findViewById(R.id.texture);
        return dialog;
    }

    public static void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
//            int width = (int) context.getResources().getDimension(R.dimen.dp_114);
            layoutParams.gravity = 17;
//            layoutParams.width = width;
            dialog.setCanceledOnTouchOutside(false);
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(17);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        }
    }

    public static void updateShowStatus(int status) {
        MyLog.logd("录音控件 改变状态 " + status);
        coverStatus(status);
    }

//    public static void updateRecordTime(long time) {
//        String indexStr;
//        if (view != null && dialog.isShowing()) {
//            try {
//                TextView textView = view.findViewById(R.id.im_tv_last_time);
//                if (textView.getVisibility() == View.VISIBLE) {
//                    if (((int) time) < 10) {
//                        indexStr = "0" + ((int) time);
//                    } else {
//                        indexStr = ((int) time) + "";
//                    }
//                    textView.setText(String.format(CommonUtil.getContext().getString(R.string.im_voice_record_last_time), indexStr));
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

    public static void updateRecordMaxAmplitude(int litude) {
        wxVoiceButton.addVoiceSize(litude);
    }

    private static void coverStatus(int status) {
        if (view != null && dialog.isShowing()) {
            LinearLayout im_voice_up_cancel = view.findViewById(R.id.im_voice_up_cancel);
            RelativeLayout im_voice_short = view.findViewById(R.id.im_voice_short);
            RelativeLayout im_voice_release_cancel = view.findViewById(R.id.im_voice_release_cancel);
            im_voice_up_cancel.setVisibility(View.INVISIBLE);
            im_voice_short.setVisibility(View.INVISIBLE);
            im_voice_release_cancel.setVisibility(View.INVISIBLE);
            switch (status) {
                case STATUS_NOMAL:
                    im_voice_up_cancel.setVisibility(View.VISIBLE);
                    return;
                case STATUS_UP_CANCEL:
                    im_voice_release_cancel.setVisibility(View.VISIBLE);
                    return;
                case STATUS_UP_SHORT:
                    im_voice_short.setVisibility(View.VISIBLE);
                    TextView textView = view.findViewById(R.id.im_voice_tv_short);
                    textView.setText(CommonUtil.getContext().getString(R.string.im_voice_time_short));
                    return;
                case STATUS_RECORD_ERROR:
                    im_voice_short.setVisibility(View.VISIBLE);
                    TextView tv = view.findViewById(R.id.im_voice_tv_short);
                    tv.setText(CommonUtil.getContext().getString(R.string.im_voice_record_error));
                    return;
                default:
                    return;
            }
        }
    }

    public static void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public static void destroy() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            dialog = null;
            view = null;
            context = null;

            if (wxVoiceButton != null) {
                wxVoiceButton.quit();
                wxVoiceButton = null;
            }
        }
    }
}
